package form

import "math"

type PaginationRequest struct {
    Page    int `json:"page" form:"page" query:"page" validate:"required,min=1"`
    PerPage int `json:"per_page" form:"per_page" query:"per_page" validate:"required,min=1,max=200"`
}

type PaginationResponse struct {
    PaginationRequest
    Total     int64 `json:"total"`
    PageTotal int64 `json:"page_total"`
}

func (m PaginationRequest) ToResponse(total int64) PaginationResponse {
    pageTotal := math.Ceil(float64(total / int64(m.PerPage)))
    return PaginationResponse{PaginationRequest:m, Total: total, PageTotal: int64(pageTotal)}
}

func (m *PaginationRequest) FillDefaultPage() {
    if m.Page < 1 {
        m.Page = 1
    }
    if m.PerPage < 1 || m.PerPage > 200 {
        m.PerPage = 10
    }
}

func (m PaginationRequest) GetOffset() int {
    return m.Page * m.PerPage - m.PerPage
}